/*
 * FBPwn
 * 
 * http://code.google.com/p/fbpwn
 * 
 * Copyright (C) 2011 - FBPwn
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package fbpwn.plugins.ui;

import fbpwn.core.FacebookAccount;
import java.awt.Component;
import java.awt.Desktop;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Toolkit;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.util.ArrayList;
import javax.swing.DefaultCellEditor;
import javax.swing.ImageIcon;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

public class ProfileCloningDialog extends javax.swing.JDialog {

    ArrayList<FacebookAccount> friendsList;

    /** Creates new form AddVictimsFriendsDialog */
    public ProfileCloningDialog(java.awt.Frame parent, boolean modal, final ArrayList<FacebookAccount> friendsList) {
        super(parent, modal);
        initComponents();

        jTable1.setCellSelectionEnabled(false);
        jTable1.setColumnSelectionAllowed(false);
        jTable1.setRowSelectionAllowed(true);
        jTable1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
        setLocation((int) (dim.getWidth() / 2 - getWidth() / 2), (int) (dim.getHeight() / 2 - getHeight() / 2));

        JCheckBox jcheckbox = new JCheckBox();
        jcheckbox.setHorizontalAlignment(SwingConstants.CENTER);
        jTable1.getColumn(jTable1.getColumnName(0)).setCellEditor(new DefaultCellEditor(jcheckbox));
        jTable1.getColumn(jTable1.getColumnName(1)).setMaxWidth(90);
        jTable1.getColumn(jTable1.getColumnName(1)).setCellRenderer(new CloneLableTableCellRenderer(this));
        jTable1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        jTable1.setRowHeight(90);
        this.friendsList = friendsList;

        Thread iconsResolver = new Thread(new Runnable() {

            @Override
            public void run() {
                for (int i = 0; i < friendsList.size(); i++) {
                    try {

                        ImageIcon ico = new ImageIcon(new URL(friendsList.get(i).getProfilePhotoURL()));
                        Image newResizedIcon = ico.getImage().getScaledInstance(90, 90, java.awt.Image.SCALE_SMOOTH);
                        friendsList.get(i).setProfilePicture(new ImageIcon(newResizedIcon));
                        ((DefaultTableModel) jTable1.getModel()).fireTableCellUpdated(i, 1);
                    } catch (MalformedURLException ex) {
                    }
                }
            }
        });
        iconsResolver.start();


        DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
        for (FacebookAccount fbAccount : friendsList) {
            model.addRow(new Object[]{fbAccount.getName(),
                        fbAccount.getProfilePhotoURL()});
        }
    }

    public ImageIcon getIcon(int index) {
        synchronized (friendsList) {
            return friendsList.get(index).getProfilePicture();
        }
    }

    public FacebookAccount showSelectionDialog() {
        setModal(true);
        setVisible(true);
        FacebookAccount selectedAccount = null;
        if (jTable1.getSelectedRow() != -1) {
            selectedAccount = friendsList.get(jTable1.getSelectedRow());
        }
        return selectedAccount;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("Clone victim's Friends");
        setResizable(false);

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Name", "Image"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTable1.setColumnSelectionAllowed(true);
        jTable1.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jTable1.getTableHeader().setReorderingAllowed(false);
        jScrollPane1.setViewportView(jTable1);
        jTable1.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);

        jButton1.setText("Submit");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton4.setText("Open In Browser");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 605, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton4, javax.swing.GroupLayout.DEFAULT_SIZE, 140, Short.MAX_VALUE)
                    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 342, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(272, 272, 272)
                        .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 33, Short.MAX_VALUE)))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    dispose();
}                                        

private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    if (jTable1.getSelectedRowCount() == 0) {
        JOptionPane.showMessageDialog(this,
                "You need to select a profile first",
                "Error Occurred",
                JOptionPane.ERROR_MESSAGE);
        return;
    }
    try {
        Desktop.getDesktop().browse(new URI(friendsList.get(jTable1.getSelectedRow()).getProfilePageURL()));
    } catch (Exception ex) {
        JOptionPane.showMessageDialog(this,
                "Failed to open browser",
                "Error Occurred",
                JOptionPane.ERROR_MESSAGE);
    }
}                                        
    // Variables declaration - do not modify
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    // End of variables declaration
}

class CloneLableTableCellRenderer extends DefaultTableCellRenderer {

    ProfileCloningDialog parentDiag = null;

    public CloneLableTableCellRenderer(ProfileCloningDialog parentDiag) {
        this.parentDiag = parentDiag;
    }

    public Component getTableCellRendererComponent(JTable table, Object value,
            boolean isSelected, boolean hasFocus, int row, int column) {

        JLabel label = (JLabel) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
        label.setHorizontalAlignment(SwingConstants.CENTER);
        label.setText("");
        label.setIcon(parentDiag.getIcon(row));
        return label;
    }
}
